﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace DMSClient
{
    public partial class ConversionList : Form
    {
        private DMSServices.DMSServicesClient client;
        private DataTable dtLisConversion;
        DataSet ds = new DataSet();
        string ambiente;
        EndpointAddress address;
        WSHttpBinding Binding = new WSHttpBinding();

        public ConversionList()
        {
            InitializeComponent();
            Binding = new WSHttpBinding();
            ambiente = Properties.Settings.Default.CurrentEnv;
            Binding.MaxReceivedMessageSize = 2147483647;
            Binding.MaxBufferPoolSize = 2147483647;
            Binding.ReceiveTimeout = TimeSpan.MaxValue;
            Binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
            Binding.Security.Mode = SecurityMode.None;
            address = new EndpointAddress(Properties.Settings.Default.Server);
           
            this.Text = Properties.Settings.Default.CurrentEnvDes + " - Conversion Table";
        }

        private void ConversionList_Load(object sender, EventArgs e)
        {
             estado(false);
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("idCurrencyFrom", "CurrencyFrom");
             ugConversionTable.DisplayLayout.Bands[0].Columns["idCurrencyFrom"].Hidden = true;
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("cdCurrencyFrom", "From");
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("idCurrencyTo", "CurrencyTo");
             ugConversionTable.DisplayLayout.Bands[0].Columns["idCurrencyTo"].Hidden = true;
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("cdCurrencyTo", "To");
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("nrYear", "Year");
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("vlConversion", "Value");
             ugConversionTable.DisplayLayout.Bands[0].Columns.Add("dtLastModified", "Last modified date");

             ConversionWorker.RunWorkerAsync();
          }

        public void estado(bool estado)
        {
            loader.Visible = !estado;
        }

        private void ConversionWorker_DoWork(object sender, DoWorkEventArgs e)
        {

            DMSServices.DMSServicesClient client;
            client = new DMSServices.DMSServicesClient(Binding, address);
            string lista = "";
            
            try
            {
                client.Open();
                lista = client.listConversion(ambiente);
                client.Close();
                if (ConversionWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
            if (lista == null)
            {
                throw new Exception("Error");
            }

            dtLisConversion = new DataTable("Conversiondt");
            dtLisConversion.Columns.Add("idCurrencyFrom");
            dtLisConversion.Columns.Add("cdCurrencyFrom");
            dtLisConversion.Columns.Add("idCurrencyTo");
            dtLisConversion.Columns.Add("cdCurrencyTo");
            dtLisConversion.Columns.Add("nrYear");
            dtLisConversion.Columns.Add("vlConversion");
            dtLisConversion.Columns.Add("dtLastModified");
            DataSet ds = new DataSet();
            ds.Tables.Add(dtLisConversion);

            System.IO.StringReader xmlSR = new System.IO.StringReader(lista);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);

            xmlSR.Close();
        }

        private void ConversionWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (!this.IsDisposed)
                {
                    this.BringToFront();
                    MessageBox.Show(this, e.Error.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    estado(true);  
                }
            }
            else if (!e.Cancelled && !this.IsDisposed)
            {
                ugConversionTable.DataSource = dtLisConversion;
                loader.Visible = false;
                estado(true);
            }
        }

        private void ConversionList_Shown(object sender, EventArgs e)
        {
            loader.Left = (this.Width - loader.Width) / 2;
            loader.Top = ((this.Height - loader.Height) / 2) - loader.Height;
        }

        private void ConversionList_Resize(object sender, EventArgs e)
        {
            loader.Left = (this.Width - loader.Width) / 2;
            loader.Top = ((this.Height - loader.Height) / 2) - loader.Height;
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            if (ugConversionTable.Rows.Count < 1)
            {
                MessageBox.Show(this, "There is no data to export", "Conversion Table Report", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Windows.Forms.SaveFileDialog dialogo = new System.Windows.Forms.SaveFileDialog();
            dialogo.DefaultExt = "xls";
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter Exporter = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
            if (dialogo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Exporter.Export(ugConversionTable, dialogo.FileName);
                if (MessageBox.Show(this, "¿Desea abrir el archivo?", "Conversion Table Report", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(dialogo.FileName);
                }
            }
        }
    }
}
